Skip to main content
Version: Latest (Core: 0.60.x, Azure: 0.46.x)

Convenient method generation

This page documents how to customize method generation for the emitters. For an overview of the setup, please visit the setup page.

Default behaviors​

By default, any language code generator will generate both protocol methods and convenient methods.

NOTE: Python and Typescript don't have a separation of convenient/protocol methods.

namespace PetStoreNamespace;

@doc("This is the input I need")
@resource("output")
model OutputModel {
@key
@doc("Id of this object")
@visibility("read")
name: string;
}

@doc("Read my resource")
op GetModel is ResourceRead<OutputModel>;

Customizations​

The detailed generation configuration of protocol and/or convenient methods that can be done:

As emitters global parameters:

  • generate-protocol-methods: boolean flag to shift the entire generation for the process (true by default)
  • generate-convenience-methods: boolean flag to shift the entire generation for the process (true by default)

To set global emitters parameters, read the documentation of emitters configuration.

For fine tuning, the set of decorators @protocolAPI and @convenientAPI can be used. They take a required boolean as parameter.

Shifting the generation of protocol and convenience on and off​

This can be achieved with the augment operator and the emitter package

import "./main.tsp";
import "@azure-tools/typespec-client-generator-core";

using Azure.ClientGenerator.Core;

@@convenientAPI(PetStoreNamespace.GetModel, false);

Make methods private/internal​

Sometimes it may be useful to still generate the method, but to make it private, so it can be re-used by a manual code wrapper.

import "./main.tsp";
import "@azure-tools/typespec-client-generator-core";

using Azure.ClientGenerator.Core;

@@access(PetStoreNamespace.GetModel, "internal");

The two possible value for the Access enum are internal and public.

Decide the usage of a model​

Models can be used for input, output, or both at the same time. In some languages, this changes the way the API is exposed for those models.

By default, the code generator will infer the usage based on the TypeSpec. If this inference doesn't correspond to expectation, this can be customized with the usage decorator. Possible values are input and output, and can be combined with Usage.input | Usage.output.

NOTE: If a model is never used, it will not be generated. Assigning a usage will force generation.

import "./main.tsp";
import "@azure-tools/typespec-client-generator-core";

using Azure.ClientGenerator.Core;

// This model is input only
@@usage(Azure.OpenAI.AzureCognitiveSearchIndexFieldMappingOptions, Usage.input);
// This models is input/output
@@usage(Azure.OpenAI.ImageGenerations, Usage.input | Usage.output);